# 热题100 https://leetcode.cn/studyplan/top-100-liked/
# 3/100 哈希-最长连续序列
# leetcode第128题: https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked
# Date: 2024/11/13

def longestConsecutive(nums: list[int]) -> int:
    s = set(nums)
    max_len = 0
    for num in s:
        if num - 1 not in s:
            cur, cur_len = num, 1

            while cur + 1 in s:
                cur += 1
                cur_len += 1
            max_len = max(max_len, cur_len)

    return max_len


if __name__ == '__main__':
    print(longestConsecutive([100, 4, 200, 1, 3, 2]))  # 4
    print(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))  # 9
